package com.easy.common.util;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTValidator;
import cn.hutool.jwt.signers.JWTSignerUtil;
import io.jsonwebtoken.*;
import io.jsonwebtoken.security.Keys;

import javax.crypto.SecretKey;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class JWTUtil {

	public static final String SECRET_KEY = "Easy";
	private static final long EXPIRATION = 86400000; // 24小时

	public static String generateToken(Map map) {
		return JWT.create().setExpiresAt(new Date(System.currentTimeMillis()+EXPIRATION))
				.setKey(SECRET_KEY.getBytes())
				.sign();


	/*	System.out.println(SECRET_KEY);
		return Jwts.builder()
				.setSubject(username)
				.claim("roles", roles)
				.setIssuedAt(new Date())
				.setExpiration(new Date(System.currentTimeMillis() + EXPIRATION))
				.signWith(SignatureAlgorithm.HS256, SECRET_KEY)
				.compact();*/
	}

	public static Object parseToken(String token) {
		/*return Jwts.parser()
				.setSigningKey(SECRET_KEY)
				.parseClaimsJws(token)
				.getBody();*/
		JWT jwt = cn.hutool.jwt.JWTUtil.parseToken(token);
		System.out.println(jwt);
		return jwt;
	}

	public boolean verifyToken(String token) {
		if (!cn.hutool.jwt.JWTUtil.verify(token,SECRET_KEY.getBytes())){
			throw new RuntimeException("签名异常");
		}

		try {
			JWTValidator.of(token).validateDate();
		}catch (Exception e){
			throw new RuntimeException("签名过期");
		}
		return true;
	}
}